package interpreterPattern;

/**
 * 解析器模式
 */
public class Client {

    public static void main(String[] args) {
        // 创建环节对象
        Context context = new Context();

        Variable a = new Variable("a");
        Variable b = new Variable("b");
        Variable c = new Variable("c");
        Variable d = new Variable("d");

        // 变量存储到对象里
        context.assign(a, 1);
        context.assign(b, 2);
        context.assign(c, 3);
        context.assign(d, 4);

        // 获取抽象语法树    a + b - c + d
        Expression expression = new Minus(new Plus(new Value(context.getValue(a)), new Value(context.getValue(b)))
                , new Plus(new Value(context.getValue(c)), new Value(context.getValue(d))));
        int interpret = expression.interpret(context);
        System.out.println(expression.toString() + " = " + interpret);

    }
}
